package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IAffirmOrderActivityModel;
import com.wgts.wgts.mvp.presenter.IAffirmOrderActivityPresenter;
import com.wgts.wgts.mvp.view.IAffirmOrderActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.AffirmOrderProduct;
import com.wgts.wgts.netmodle.req.AffirmOrderRequest;
import com.wgts.wgts.netmodle.req.OrderLeaveMessage;
import com.wgts.wgts.netmodle.req.PlaceOrderRequest;
import com.wgts.wgts.netmodle.resp.AffirmOrderEntity;
import com.wgts.wgts.netmodle.resp.OrderEntity;
import com.wgts.wgts.thirdParty.pay.PayTools;

import java.util.ArrayList;
import java.util.List;

/*
 *
 *
 *   data:2018/10/615:01
 *
 */public class AffirmOrderActivityPresenter implements IAffirmOrderActivityPresenter {
     private IAffirmOrderActivityView view;
     private IAffirmOrderActivityModel model;
     private AffirmOrderEntity entity;
     private List<AffirmOrderEntity.shop> list;

     //支付商品数据
    private PlaceOrderRequest placeOrderRequest;
    private AffirmOrderProduct product;

    public AffirmOrderActivityPresenter(IAffirmOrderActivityView view, IAffirmOrderActivityModel model) {
        this.view = view;
        this.model = model;
    }

    @Override
    public void affirmOrderResult(final AffirmOrderRequest requests) {
        model.affirmOrderResult(requests, new HttpResultSubscriber<AffirmOrderEntity>() {
            @Override
            public void onSuccess(AffirmOrderEntity affirmOrderEntity) {
                entity = new AffirmOrderEntity();
                entity = affirmOrderEntity;
                list = entity.shops;
                view.affirmOrderResult(entity);
                placeOrderRequest = new PlaceOrderRequest();
                placeOrderRequest.products = new ArrayList<>();
                for (int i = 0; i < list.size(); i++){
                    for (int j = 0; j < list.get(i).products.size(); j++){
                        product = new AffirmOrderProduct();
                        product.productId = list.get(i).products.get(j).productId;
                        product.productNum = list.get(i).products.get(j).productCount;
                        product.skuId = list.get(i).products.get(j).skuId;
                        placeOrderRequest.products.add(product);
                    }
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void getShopList(boolean isAll) {
        if (entity != null){
            if (isAll){
                for (int i = 0; i < list.size(); i++){
                    list.get(i).show = true;
                    for (int j = 0; j < list.get(i).products.size(); j++){
                        list.get(i).products.get(j).show = true;
                    }
                }
                view.getShopList(list);
            }else {
                for (int i = 0; i < list.size(); i++){
                    list.get(i).show = false;
                    for (int j = 0; j < list.get(i).products.size(); j++){
                        list.get(i).products.get(j).show = false;
                    }
                }
                list.get(0).show = true;
                list.get(0).products.get(0).show = true;
                view.getShopList(list);
            }
        }
    }

    @Override
    public void placeOrder(AffirmOrderRequest request, String addressId, final String payType,
                           String payPassword, List<Integer> cartIds, List<OrderLeaveMessage> orderLeaveMessages) {
        placeOrderRequest.products = request.products;
        placeOrderRequest.addressId = addressId;
        placeOrderRequest.payType = payType;
        placeOrderRequest.payPassword = payPassword;
        placeOrderRequest.cartIds = cartIds;
        placeOrderRequest.orderTruePrice = entity.orderTruePrice;
        placeOrderRequest.orderLeaveMessages = orderLeaveMessages;
        model.placeOrder(placeOrderRequest, new HttpResultSubscriber<OrderEntity>() {
            @Override
            public void onSuccess(OrderEntity orderEntity) {
                view.placeOrder(payType, orderEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

}
